public class AssignStatement extends Statement
{

	private String varName;
	private Expression expression;

	public DataInfo dataInfo;

	public AssignStatement( String name, Expression exp, DataInfo dataInfo )
	{

		this.varName = name;
		this.expression = exp;
		this.dataInfo = dataInfo;

	}

	public String toString()
	{

		String result = "Assignstmt " + varName + " \n";
		String tbr = "| " + expression;

		for( String s : tbr.split( "\n" ) )
		{
			result += "| " + s + "\n";
		}
		return result;

	}

	public void check() throws SemanticException
	{

		SymbolTable st = SymbolTable.getInstance();
		this.expression.check();
		if( st.contains( varName ) )
		{

			String varType = st.get( varName ).type;
			String expType = expression.getType();
			if( !varType.equalsIgnoreCase( expType ) )
			{
				String details = "The types of left-hand side and right-hand side of the assign statement do not match.";
				if( dataInfo != null )
				{
					throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, details );
				}
				else
				{
					throw new SemanticException( details );
				}
			}

		}
		else
		{
			String details = "The left-hand side of the assign statement has not been declared.";
			if( dataInfo != null )
				throw new SemanticException( dataInfo.lineNumber, dataInfo.charNumber, dataInfo.line, details );
			else
				throw new SemanticException( details );
		}
	}

}
